<template>
  <div class="BillManage">
    <div class="head">
      <h1 class="title">账单管理</h1>
    </div>
    <div class="container">
      <div>
        <el-button class="createBillBTN" type="primary" size="middle" @click="showCreateBillDialog" plain>创建账单</el-button>
        <el-table :data="bills">
          <el-table-column label="账单ID" prop="billId" width="140px"></el-table-column>
          <el-table-column label="账单名称" prop="billName" width="140px"></el-table-column>
          <el-table-column label="金额" prop="amount" width="140px"></el-table-column>
          <el-table-column label="支付状态" prop="status" width="140px"></el-table-column>
          <el-table-column label="操作" width="200px">
            <template v-slot="scope">
              <el-button @click="payBill(scope.row)" type="primary" size="small">支付</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-dialog title="创建账单" :visible.sync="isCreateBillDialogVisible">
          <el-form :model="newBill">
            <el-form-item label="账单名称">
              <el-input v-model="newBill.billName"></el-input>
            </el-form-item>
            <el-form-item label="金额">
              <el-input v-model="newBill.amount"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="isCreateBillDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="createBill">确定</el-button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>
<script>
import axios from 'axios';
import { ElMessage } from 'element-plus';

export default {
  data() {
    return {
      bills: [],
      isCreateBillDialogVisible: false,
      newBill: {
        billName: '',
        amount: ''
      }
    };
  },
  created() {
    this.fetchBills();
  },
  methods: {
    fetchBills() {
      axios.get('http://localhost:8085/api/bill', {
        params: { userId: this.$store.state.id }
      })
      .then(response => {
        if (response.data.code === 1) {
          this.bills = response.data.data;
        } else {
          ElMessage.error(response.data.msg);
        }
      })
      .catch(() => {
        ElMessage.error('获取账单信息失败');
      });
    },
    showCreateBillDialog() {
      this.isCreateBillDialogVisible = true;
    },
    createBill() {
      axios.post('http://localhost:8085/api/bill', {
        ...this.newBill,
        userId: this.$store.state.id
      })
      .then(response => {
        if (response.data.code === 1) {
          ElMessage.success('账单创建成功');
          this.isCreateBillDialogVisible = false;
          this.fetchBills();
        } else {
          ElMessage.error(response.data.msg);
        }
      })
      .catch(() => {
        ElMessage.error('创建账单失败');
      });
    },
    payBill(bill) {
      const form = document.createElement('form');
      form.action = 'http://bmwt9c.natappfree.cc/api/pay/showPay';
      form.method = 'post';
      form.target = '_blank';

      const outTradeNoInput = document.createElement('input');
      outTradeNoInput.name = 'WIDout_trade_no';
      outTradeNoInput.value = bill.billId;
      form.appendChild(outTradeNoInput);

      const subjectInput = document.createElement('input');
      subjectInput.name = 'WIDsubject';
      subjectInput.value = bill.billName;
      form.appendChild(subjectInput);

      const totalAmountInput = document.createElement('input');
      totalAmountInput.name = 'WIDtotal_amount';
      totalAmountInput.value = bill.amount;
      form.appendChild(totalAmountInput);

      const bodyInput = document.createElement('input');
      bodyInput.name = 'WIDbody';
      bodyInput.value = '账单支付';
      form.appendChild(bodyInput);

      document.body.appendChild(form);
      form.submit();
      document.body.removeChild(form);
    }
  }
};
</script>
<style scoped>
.head {
  text-align: center;
  margin-top: 10%;
}

.container {
  display: flex;
  margin-top: 2em;
}

.createBillBTN {
  margin-bottom: 20px;
}
</style>
